import { fetchApiWithGet, fetchApiWithPost } from '../utils/index'
import { PREVIEW_ORDER_SUCC, PREVIEW_ORDER_FAIL, CREATE_ORDER_SUCC, CREATE_ORDER_FAIL, SHOW_ORDER_SUCC, SHOW_ORDER_FAIL, SHOW_DETAIL_SUCC, SHOW_DETAIL_FAIL } from '../contants/order'

var publicUrl = 'http://fengke.net:8081/shop/';

export function previewOrder(cartIds) {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}order_preview`;
	var argus = {
		userId: userId,
		cartIds: cartIds
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, PREVIEW_ORDER_SUCC, PREVIEW_ORDER_FAIL)
	}
}

export function createOrder(cartIds,addressId) {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}order_create`;
	var argus = {
		userId: userId,
		cartIds: cartIds,
		addressId: addressId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, CREATE_ORDER_SUCC, CREATE_ORDER_FAIL)
	}
}

export function showOrder(status) {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}order_show`;
	var argus = {
		userId: userId,
		status: status
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, SHOW_ORDER_SUCC, SHOW_ORDER_FAIL)
	}
}

export function showOrderDetail(orderId) {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}order_detail`;
	var argus = {
		userId: userId,
		orderId: orderId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, SHOW_DETAIL_SUCC, SHOW_DETAIL_FAIL)
	}
}
